package com.growthsee.rider.wrapper;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.growthsee.common.utils.SecurityUtils;
import com.growthsee.rider.constant.ConstantConfig;
import com.growthsee.rider.domain.*;
import com.growthsee.rider.domain.dto.RiderAnswerDto;
import com.growthsee.rider.domain.dto.RiderQuestionDto;
import com.growthsee.rider.enumeration.CourseTypeEnum;
import com.growthsee.rider.enumeration.YesNoEnum;
import com.growthsee.rider.mapper.RiderQuestionTypeMapper;
import com.growthsee.rider.service.*;
import com.growthsee.rider.utils.ApplicationContexts;
import com.growthsee.rider.utils.BeanUtilsExt;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ycj
 * @Description: 评价活动视图包装类
 * @date 2020/05/24
 **/
public class QuestionWrapper {


    public static QuestionWrapper builder() {
        return new QuestionWrapper();
    }


    public void propertiesHandler(List<RiderQuestionDto> records) {
        if(CollectionUtils.isNotEmpty(records)){
            buildExtend(records);
            List<String>ids = records.stream().map(s->s.getId()).distinct().collect(Collectors.toList());
            RiderQuestionOptionService optionService = ApplicationContexts.getBean(RiderQuestionOptionService.class);
            List<String>questionTypeIds = records.stream().map(s->s.getTypeId()).distinct().collect(Collectors.toList());
            RiderQuestionTypeMapper questionTypeMapper = ApplicationContexts.getBean(RiderQuestionTypeMapper.class);
            List<RiderQuestionType>types =questionTypeMapper.selectList(Wrappers.<RiderQuestionType>lambdaQuery().in(RiderQuestionType::getId,questionTypeIds));
            List<RiderQuestionOption>options =optionService.list(Wrappers.<RiderQuestionOption>lambdaQuery().in(RiderQuestionOption::getQuestionId,ids).orderByAsc(RiderQuestionOption::getSort));
            for (RiderQuestionDto record : records) {
                RiderQuestionType questionType = types.stream().filter(s -> StringUtils.equals(s.getId(), record.getTypeId())).findFirst().orElse(null);
                if(!Objects.isNull(questionType)){
                    record.setTypeName(questionType.getName());
                    record.setOptionType(questionType.getOptionType());
                }
                if(CollectionUtils.isNotEmpty(options)){
                    List<RiderQuestionOption>qptions = options.stream().filter(s->StringUtils.equals(s.getQuestionId(),record.getId())).distinct().collect(Collectors.toList());
                    record.setOptions(qptions);
                }

            }
        }
    }

    /**
     * 答题
     * @param answerDto
     * @return
     */
    public void audit(RiderAnswerDto answerDto) {
        RiderCourseService riderCourseService = ApplicationContexts.getBean(RiderCourseService.class);
        RiderCourse course =riderCourseService.getOne(Wrappers.<RiderCourse>lambdaQuery().eq(RiderCourse::getId,answerDto.getCourseId()));
        RiderQuestionService riderQuestionService = ApplicationContexts.getBean(RiderQuestionService.class);
        List<String>ids = answerDto.getAnswerQuestionDtos().stream().map(s->s.getQuestionId()).collect(Collectors.toList());
        List<RiderQuestion>questions =riderQuestionService.list(Wrappers.<RiderQuestion>lambdaQuery().in(RiderQuestion::getId,ids));
        questions = CollectionUtils.isNotEmpty(questions)?questions:new ArrayList<>();
        List<RiderQuestionDto>riderQuestionDtos= BeanUtilsExt.copyList(questions,RiderQuestionDto.class);
        buildExtend(riderQuestionDtos);
        answerDto.getAnswerQuestionDtos().forEach(v->{
            RiderQuestionDto riderQuestionDto = riderQuestionDtos.stream().filter(s->StringUtils.equals(s.getId(),v.getQuestionId())).findFirst().orElse(null);
            if(!Objects.isNull(riderQuestionDto)){
                v.setContent(riderQuestionDto.getExtend().getContent());
                v.setIsTrue(isTrue(v.getAnswer(),riderQuestionDto.getAnswer()));
            }
        });
        //答对题数
        Integer correctNum =answerDto.getAnswerQuestionDtos().stream().filter(s->s.getIsTrue()==1).collect(Collectors.toList()).size();
        answerDto.setCurrentPass(correctNum);
        if(ObjectUtils.isEmpty(course.getPassScore()) || course.getPassScore().intValue()==0){
            course.setPassScore(new BigDecimal(questions.size()));
        }
        answerDto.setPassScore(course.getPassScore().intValue());

    }


    /**
     * 构建试题扩展信息
     * @param questionDtos
     */
    private void buildExtend(List<RiderQuestionDto>questionDtos){
        if(CollectionUtils.isNotEmpty(questionDtos)){
            RiderQuestionExtendService extendService = ApplicationContexts.getBean(RiderQuestionExtendService.class);
            List<String>ids = questionDtos.stream().map(s->s.getId()).distinct().collect(Collectors.toList());
            List<RiderQuestionExtend>questionExtends =extendService.list(Wrappers.<RiderQuestionExtend>lambdaQuery().in(RiderQuestionExtend::getId,ids));
            questionDtos.forEach(v->{
                if(CollectionUtils.isNotEmpty(questionExtends)){
                    RiderQuestionExtend questionExtend = questionExtends.stream().filter(s -> StringUtils.equals(s.getId(), v.getId())).findFirst().orElse(null);
                    v.setExtend(questionExtend);
                }
            });
        }
    }

    private Integer isTrue(String answer,String correctAnswer){
        List<String>answerList = Arrays.asList(answer.split(","));
        List<String>correctAnswerList = Arrays.asList(correctAnswer.split(","));
        boolean isTrue = answerList.stream().sorted().collect(Collectors.joining())
                .equals(correctAnswerList.stream().sorted().collect(Collectors.joining()));
        return isTrue?1:0;
    }
}
